package cn.edu.ujn.finaltest.controller;

import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysql.jdbc.StringUtils;

import cn.edu.ujn.finaltest.dao.Product;
import cn.edu.ujn.finaltest.dao.User;
import cn.edu.ujn.finaltest.service.IUserService;
import cn.edu.ujn.finaltest.util.Page;

@Controller
public class UserController {
	@Autowired
	private IUserService userService;	
	
	@GetMapping("/login.action")
	public String toLogin() {
	    return "login";
	}
	
	@PostMapping("/login.action")
	public String login(String usercode,String password, Model model,HttpSession session) {
		User user = userService.selectByCodePwd(usercode, password);
		if(user != null){		
			session.setAttribute("USER_SESSION", user);
			return "redirect:customer/list.action";
		}
		model.addAttribute("msg", "账号或密码错误，请重新输入！");
		return "login";
	}
	
	@RequestMapping("/logout.action")
	public String logout(HttpSession session) {
	    session.invalidate();
	    return "redirect:login.action";
	}
	
	@RequestMapping("toCustomer")
	public String toCustomer() {     //直接请求customer界面会被拦截器拦截，然后重定向回login界面
		return "customer";
	}
	
	@RequestMapping(value = "/user/list.action")
	public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")  Integer rows, Integer user_id, String user_code,String user_name,String user_password,Integer user_state,Model model) {
		Page<User> findUserlist = userService.findUserList(page, rows, user_id, user_code, user_name, user_password, user_state);
		model.addAttribute("page",findUserlist);
		model.addAttribute("user_id",user_id);
		model.addAttribute("user_code",user_code);
		model.addAttribute("user_name",user_name);
		model.addAttribute("user_password",user_password);
		model.addAttribute("user_state",user_state);
		return "user";
	}
	
	@RequestMapping(value = "/user/create.action")
	@ResponseBody
	public String userCreate(User user,Integer user_id, String user_code,String user_name,String user_password,Integer user_state, HttpSession session) {
		// 获取Session中的当前用户信息
		User user1 = (User) session.getAttribute("USER_SESSION");
		// 将当前用户id存储在客户对象中
		//product.setId(user.getUser_id());
        user.setUser_id(user_id);
        user.setUser_code(user_code);
        user.setUser_name(user_name);
        user.setUser_password(user_password);
        user.setUser_state(user_state);
		// 执行Service层中的创建方法，返回的是受影响的行数
		int rows = userService.insertSelective(user);
		if (rows > 0) {
			return "OK";
		} else {
			return "FAIL";
		}
	}
	
	@RequestMapping(value = "/user/getUser.action")
	@ResponseBody
	public User getUser(Integer id) {
		User user = userService.selectByPrimaryKey(id);
		return user;
	}
	
	@RequestMapping(value = "/user/update.action")
	@ResponseBody
	public String userUpdate(User user,String user_password_old, String user_password_new1, String user_password_new2, HttpSession session) {
		// 获取Session中的当前用户信息
		User user1 = (User) session.getAttribute("USER_SESSION");
		if(!StringUtils.isNullOrEmpty(user_password_old) && 
				!StringUtils.isNullOrEmpty(user_password_new1) && 
				!StringUtils.isNullOrEmpty(user_password_new2)) {
			if(!user_password_old.equals(user1.getUser_password())) {
				return "2";          //错误代码2: 与原密码不一致
			}
			
			if(!user_password_new1.equals(user_password_new2)) {
				return "3";           //错误代码3: 两次密码输入不一致
			}
			
			user1.setUser_password(user_password_new1);
			
			// 执行Service层中的创建方法，返回的是受影响的行数
			int rows = userService.updateByPrimaryKeySelective(user1);
			if (rows > 0) {
				return "1";              //成功代码1: 创建成功
			} else {
				return "4";              //错误代码4: 创建失败
			}
		}
		
		return "5";                     //错误代码5: 有空输入
		
	}
	
}
